package com.alex.utils.html;

import java.util.ArrayList;
import java.util.Collection;

import com.alex.utils.common.Random;


public class RadioInput extends Input {

	private ArrayList<String> options = new ArrayList<String>();

	private int selectedOption = -1;

	public RadioInput(String name) {
		super(name, "");
	}

	public void addOption(String option, boolean checked) {

		int size = options.size();
		if (checked || size == 0)
			selectedOption = size;
		options.add(option);

	}

	public Collection<String> getOptions() {
		return options;
	}

	public int getSelectedOption() {
		return selectedOption;
	}

	public void setSelectedOption(int checkedOption) {
		this.selectedOption = checkedOption;
	}

	public boolean setSelectedOption(String option) {

		int i = options.indexOf(option);
		if (i >= 0) {
			selectedOption = i;
			return true;
		}

		return false;
	}

	public String getValue() {

		if (selectedOption == -1 || options.isEmpty())
			return "";

		return options.get(selectedOption);
	}

	public void selectRandom() {
		
		int len = options.size();
		if (len <= 0) return;
		
		selectedOption = Random.getInt(len);
	}
}
